<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Using a Cursor to Get All of the Documents</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongo.tutorial.counting.html">« Counting Documents in A Collection</a></li>
      <li style="float: right;"><a href="mongo.tutorial.criteria.html">Setting Criteria for a Query »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongo.tutorial.html">Tutorial</a></li>
    <li>Using a Cursor to Get All of the Documents</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongo.tutorial.cursor" class="section">
   <h2 class="title">Using a Cursor to Get All of the Documents</h2>
   <p class="para">
    In order to get all the documents in the collection, we will use
    <span class="function"><a href="mongocollection.find.html" class="function">MongoCollection::find()</a></span>. The find() method returns a
    <a href="class.mongocursor.html" class="classname">MongoCursor</a> object which allows us to iterate over the
    set of documents that matched our query. So to query all of the documents and
    print them out:

    <div class="example" id="mongo.tutorial.cursor-example">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">();<br />foreach&nbsp;(&nbsp;</span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">:&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>

    and that should print all 101 documents in the collection.
    <code class="literal">$id</code> is the <code class="literal">_id</code> field of a document
    (cast to a string) and <code class="literal">$value</code> is the document itself.
   </p>
   <div class="section" id="mongo.tutorial.cursor.seealso">
    <h2 class="title">See Also</h2>
    <p class="para">
     The API documentation on <span class="function"><a href="mongocollection.find.html" class="function">MongoCollection::find()</a></span>
     contains more information about finding data.
    </p>
   </div>
  </div></div></div></body></html>